<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Jobs\LeaseTime;
use App\Models\Car;
use Illuminate\Http\Request;
use Modules\Car\Entities\RecognizeDrivingLicense;
use Modules\Car\Entities\CarOrder;

class CarController extends Controller
{
    use ResponseData;
    public function index(Car $car,Request $request){
        $name = trim($request->input('name',''));
        $category = $request->input('category','');
        $size = $request->input('size',10);
        $model = $car->orderBy('total_price','asc');
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        } else {
            $model = $model->where('status','<>',-1);
        }
            $model = $model->when($name,function($query)use($name){
                return $query->where('name','like','%'.$name.'%');
            });
              $data  = $model->when($category,function($query)use($category){
                  return $query->where('category',$category);
              })
            ->paginate($size);
        return $this->success($data);
    }
    public function store(Car $car,Request $request){
        $data = $request->all();
        $data['total_price'] = $request->minimum_price+$request->base_price+$request->brokerage;
        $car->fill($data);
        $car->save();
        return $this->success('保存成功');
    }
    public function edit(Car $car,Request $request){
        $data = $car->find($request->id);
        return $this->success($data);
    }
    public function update(Car $car,Request $request){
        $data = $car->find($request->id);
        if($request->filled('category')){
            RecognizeDrivingLicense::where('car_series_id',$request->id)->update(['category'=>$request->category]);
        }
        $map = $request->all();
        if ($request->filled('minimum_price')) {
            $map['total_price'] = $request->minimum_price+$request->base_price+$request->brokerage;
        }
        $data->update($map);
        return $this->success('修改成功');
    }
    public function destory(Car $car,Request $request){
        $data = $car->find($request->id);
        $data->delete();
        return $this->success('删除成功');
    }
    // 获取在租车辆
    public function leaseCar(RecognizeDrivingLicense $model,Request $request){
        $size = $request->input('size',10);
        if($request->filled('name')){
            $model = $model->where('gear_car_name','like','%'.$request->name.'%');
        }
        if($request->filled('plate_number')){
            $model->where('plate_number','like','%'.$request->plate_number.'%');
        }
        $model = $model->select('id','category','plate_number','gear_car_name');
        $model = $model->where('is_used',1)->whereNull('deleted_at');
        $data = $model->paginate($size);
        return $this->success($data);
    }
    // 获取所有车牌号
    public function getAllPlateNumber(){
        $source = RecognizeDrivingLicense::query()->where('id','>',0)->whereNull('deleted_at')->pluck('plate_number');
        $res = $source->values()->toArray();
        $res = array_values($res);
        return $this->success($res);
    }
    // 车辆排期表
    public function scheduling(RecognizeDrivingLicense $model,Request $request){
        $size = $request->input('size',10);
        if ($request->filled('plate_number')) {
            $model = $model->whereIn('plate_number',$request->plate_number);
        }
        if ($request->filled('category')) {
            $model = $model->where('category',$request->category);
        }
        $model = $model->whereNull('deleted_at')
        ->with(['orders'=>function($query){
            $query->where('status',5)->whereIn('process_status',[1,2]);
        }]);
        // $model = $model->whereNull('deleted_at')->with('orders','renews');
        $data = $model->paginate($size);
        return $this->success($data);
    }
}
